package com.wtwd.campus.receiver;

import com.wtwd.campus.entity.DeviceSOSList;
import com.wtwd.campus.entity.MqAllSosResult;
import com.wtwd.campus.entity.MqLocationData;
import com.wtwd.campus.service.IRMFeignClientService;
import com.wtwd.campus.service.InjectFeignService;
import com.wtwd.campus.utils.LocationUtils;
import com.wtwd.campus.utils.UnicodeUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @author mjy
 * @date 2021/07/20
 * @description
 */
public class setAllSosReceiver implements Receiver {

    @Autowired
    private IRMFeignClientService irmFeignClientService;

    @Override
    public String parseMessage(String data) {
        //解析到指令：*WT,868976030203014,V4,SETSOS,162040,162010,1,\u6d4b\u0031,18797874506,2,\u6d4b\u0032,13669061320,3,\u6d4b\u0032,13669061323,V,0.0000,S,0.0000,W,0.00,0,210721,FDFFFFFF#
        String[] split = data.split(",");
        int length = split.length;
        String imei = split[1];//imei号
        String messageType = split[2];
        String type = split[3];
        String commandNumber = split[4];//流水号
        List<DeviceSOSList> deviceSOSLists = new ArrayList<>();
        //固定数据一定是3组(解析参数)
        List<Integer> sequenceList = new ArrayList<>();
        for (int j = 1; j <= 3; j++) {
            //假如是第一组 split[6]
            sequenceList.add(Integer.parseInt(split[5 + (j - 1) * 3 + 1]));
            DeviceSOSList deviceSOSList = new DeviceSOSList();
            //这里只存发送的参数，name,phone,sequence
            deviceSOSList.setSequence(Integer.parseInt(split[5 + (j - 1) * 3 + 1]));
            String name = split[5 + (j - 1) * 3 + 2];
            if (StringUtils.isNotBlank(name)) {
                name = UnicodeUtil.decodeUnicode(name);
            }
            deviceSOSList.setSosName(name);
            deviceSOSList.setSosPhone(split[5 + (j - 1) * 3 + 3]);
            deviceSOSLists.add(deviceSOSList);
        }
        String time = split[5];
        //年月日
        String date = split[length - 2];
        String orientation = split[length - 3];
        String speed = split[length - 4];
        String lng = split[length - 6];
        String lat = split[length - 8];
        //设备状态
        String deviceStatus = split[length - 1];
        List<Double> list = LocationUtils.transiformLatAndLng(lng, lat);
        MqAllSosResult mqAllSosResult = new MqAllSosResult();
        mqAllSosResult.setCommandNumber(commandNumber);
        mqAllSosResult.setDeviceDate(date);
        mqAllSosResult.setDeviceTime(time);
        mqAllSosResult.setImei(imei);
        MqLocationData mqLocationData = new MqLocationData();
        mqLocationData.setOrientation(Integer.parseInt(orientation));
        mqLocationData.setSpeed(Double.parseDouble(speed));
        mqLocationData.setLatitude(list.get(1));
        mqLocationData.setLongitude(list.get(0));
        mqAllSosResult.setMqLocationData(mqLocationData);
        mqAllSosResult.setSosCallList(deviceSOSLists);
        InjectFeignService.getIrmFeignClientService().allSosResponse(mqAllSosResult);
        return imei;
    }


}
